package main;

import generated.Person;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Main {

    public static void main(String[] args) {
        Main main = new Main();
        main.do_Marshall();
        main.do_UnMarshall();
    }

    public void do_Marshall() {
        Person person = new Person();
        person.setFirstname("first name");
        person.setLastname("last name");
        

        try {

            File outputFile = new File("./src/person.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            marshaller.marshal(person, outputFile);            

        } catch (JAXBException e) {
            
        }
    }
    
    public void do_UnMarshall() {
        try {

            File inputFile = new File("./src/person.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
            Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();

            Person person = (Person) unMarshaller.unmarshal(inputFile); 
            
            System.out.println(person.getFirstname());
            System.out.println(person.getLastname());

        } catch (JAXBException e) {
            
        }
    }
}

